#include "LCD.h"

#define BLACK 0x00000000 // 黑色
#define WHITE 0x00ffffff // 白色

#define WIDTH 800
#define HEIGHT 480

// 显示屏初始化以及清屏
int lcd_clear()
{
    int lcd_fd = open("/dev/fb0", O_RDWR);
    if (lcd_fd < 0)
    {
        perror("Failed to open frame buffer device");
        return -1;
    }

    int(*lcd_mmap)[WIDTH] = (int(*)[WIDTH])mmap(NULL, WIDTH * HEIGHT * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd_fd, 0);
    if (lcd_mmap == MAP_FAILED)
    {
        perror("mmap failed");
        close(lcd_fd);
        return -1;
    }

    // 清屏 (默认填充白色)
    for (size_t row = 0; row < HEIGHT; row++)
    {
        for (size_t col = 0; col < WIDTH; col++)
        {
            lcd_mmap[row][col] = WHITE;
        }
    }

    munmap(lcd_mmap, WIDTH * HEIGHT * 4);
    close(lcd_fd);

    return 0;
}
